package com.woniuxy.stage3demo.controller.gy;

import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.StaffBean;
import com.woniuxy.stage3demo.bean.gy.CollectionLogBean;
import com.woniuxy.stage3demo.bean.hpxbean.OrderBean;
import com.woniuxy.stage3demo.service.gy.OverdueCollectionLogService;
import com.woniuxy.stage3demo.service.gy.OverdueOrderService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**邮件提醒
 * @author ：苟杨
 * @description：TODO
 * @date ：2020/12/12 17:15
 */
@RequestMapping("gy")
@RestController
public class MailController {
    @Resource
    private JavaMailSender mailSender;
    @Resource
    private OverdueOrderService overdueOrderService;
    @Resource
    private OverdueCollectionLogService overdueCollectionLogService;
    //群发催收
    @GetMapping(value ="/mail", produces = {"application/json;charset=utf-8"})
    public ResultMsg sendMail(String region,String email,
                              int orderbeanId,String collectionType,String collObj){
        System.out.println(region+"...."+"...."+email+"这里是提醒表"+orderbeanId+"..."+"..."+collectionType+"..."+collObj);
        OrderBean orderBean = overdueOrderService.findById(orderbeanId);
        CollectionLogBean cl = new CollectionLogBean();
        cl.setTelphone(email);
        cl.setResult("承诺还款");
        cl.setCollObj(collObj);
        cl.setCollectionType(collectionType);
        cl.setOrderBean(orderBean);
        StaffBean sb = new StaffBean();
        sb.setId(1);
        cl.setStaffBean(sb);
        overdueCollectionLogService.add(cl);
        //群发邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("gouyang202012@163.com");
        message.setTo(email);
        message.setSubject("[蜗牛金融]提醒您:");
        message.setText(region);
        mailSender.send(message);
        ResultMsg rm = new ResultMsg<>();
        rm.setCode(200);
        return rm;
    }
    //对详情页的催收
    @GetMapping(value ="/mailPerson", produces = {"application/json;charset=utf-8"})
    public ResultMsg sendMail(String region,int orderbeanId, String collectionType,
                              String collObj, String telphone){
        //增加催收历史信息
        OrderBean orderBean = overdueOrderService.findById(orderbeanId);
        CollectionLogBean cl = new CollectionLogBean();
        cl.setTelphone(telphone);
        cl.setResult("承诺还款");
        cl.setCollObj(collObj);
        cl.setCollectionType(collectionType);
        cl.setOrderBean(orderBean);
        StaffBean sb = new StaffBean();
        sb.setId(1);
        cl.setStaffBean(sb);
        overdueCollectionLogService.add(cl);
        //发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("gouyang202012@163.com");
        message.setTo(telphone);
        message.setSubject("还呗提醒您");
        message.setText(region);
        mailSender.send(message);
        ResultMsg rm = new ResultMsg<>();
        rm.setCode(200);
        return rm;
    }
}
